import { useEffect, useMemo, useState } from 'react';
import { useSelector, shallowEqual } from 'react-redux';
import { IReducerState, IRootReducerState } from '@/store/root-reducer';
import { Button, Popconfirm, Space, Table, Toast } from '@douyinfe/semi-ui';
import {
  IconDeleteStroked,
  IconEditStroked,
  IconSearchStroked,
  IconInheritStroked,
} from '@douyinfe/semi-icons';
import { ColumnProps } from '@douyinfe/semi-ui/lib/es/table';
import { PaginationProps } from '@douyinfe/semi-ui/lib/es/pagination';
import { usePageContext } from '../context';
import TablePagination, {
  onPageParamsChange,
} from '@/widgets/table-pagination';
import { pageSizeOpts } from '../helper';
import BaseColumns from './columns';
import { deleteItem } from '@/redux/organize/organize-api';
import { fetchItems } from '@/redux/organize/organize-actions';
import { useDispatch } from 'react-redux';

const organizeTable = () => {
  const dispatch = useDispatch<any>();
  const PageContext = usePageContext();
  const PageAction = useMemo(() => {
    return {
      queryParams: PageContext.queryParams,
      setQueryParams: PageContext.setQueryParams,
      addRecord: PageContext.addRecord,
      editRecord: PageContext.editRecord,
      allocationRecord: PageContext.allocationRecord,
      delRecord: PageContext.delRecord,
      detailRecord: PageContext.detailRecord,
    };
  }, [PageContext]);

  const { organize } = useSelector<IRootReducerState, any>(
    (state) => ({ organize: state.organize }),
    shallowEqual
  );
  const { entities, totalCount } = organize;

  const columns: ColumnProps[] = useMemo(() => {
    return [
      ...BaseColumns,
      {
        title: '操作',
        width: 300,
        dataIndex: 'action',
        render: (text, record, index) => {
          return (
            <Space>
              <Button
                icon={<IconEditStroked />}
                onClick={() => {
                  PageAction.editRecord(record[organize.key]);
                }}></Button>
              <Button
                icon={<IconSearchStroked />}
                onClick={() => {
                  PageAction.detailRecord(record[organize.key]);
                }}></Button>
              <Button
                icon={<IconInheritStroked />}
                onClick={() => {
                  PageAction.detailRecord(record[organize.key], 'allocation');
                }}></Button>
              {/* {record?.status === 1 ? ( */}
              <Popconfirm
                title='请注意'
                content='确定删除该组织?'
                onConfirm={() => {
                  dispatch(deleteItem(record[organize.key])).then(
                    (res: any) => {
                      dispatch(fetchItems(PageAction.queryParams));
                    }
                  );
                }}>
                <span className='inline-block invisible group-hover/item:visible'>
                  <Button
                    icon={<IconDeleteStroked />}
                    type='danger'
                    onClick={() => {
                      // PageAction.delRecord(record[application.key]);
                    }}></Button>
                </span>
              </Popconfirm>
              {/* ) : null} */}
            </Space>
            // <Space>
            //   <Button
            //     theme={'borderless'}
            //     onClick={() => {
            //       PageAction.editRecord(record[organize.key]);
            //     }}
            //   >
            //     修改
            //   </Button>
            //   <Popconfirm title='系统提示' content='是否删除所选中数据?'>
            //     <span style={{ display: 'inline-block' }}>
            //       <Button
            //         theme={'borderless'}
            //         onClick={() => {
            //           PageAction.delRecord(record[organize.key]);
            //         }}
            //       >
            //         删除
            //       </Button>
            //     </span>
            //   </Popconfirm>
            //   <Button
            //     theme={'borderless'}
            //     onClick={() => {
            //       PageAction.detailRecord(record[organize.key]);
            //     }}
            //   >
            //     查看
            //   </Button>
            //   <Button
            //     theme={'borderless'}
            //     onClick={() => {
            //       PageAction.allocationRecord(record[organize.key]);
            //     }}
            //   >
            //     分配应用
            //   </Button>
            // </Space>
          );
        },
        // // 此处从render的第四个形参中解构出 展开按钮、选择按钮、文本等内容
        // render: (text, record, index, { expandIcon, selection, indentText }) => {
        //   return <>111</>;
        // })
      },
    ];
  }, []);

  const onRowEvent: any = useMemo(
    () => (record: any, index: number) => {
      return {
        className: 'group/item',
      };
    },
    []
  );

  const paginationOptions = useMemo(
    () => ({
      total: totalCount,
      queryParams: PageAction.queryParams,
      pageSizeOpts,
    }),
    [PageAction, organize]
  );

  return (
    <TablePagination paginationOpts={paginationOptions}>
      {(paginationOpts: PaginationProps) => (
        <Table
          size='small'
          columns={columns}
          dataSource={entities}
          onChange={(params: any) =>
            onPageParamsChange(params, PageAction.setQueryParams)
          }
          pagination={paginationOpts}
          onRow={onRowEvent}
        />
      )}
    </TablePagination>
  );
};

export default organizeTable;
